package io.renren.modules.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.shop.entity.GoodsMsg;
import io.renren.modules.shop.entity.GoodsPic;
import io.renren.modules.shop.service.GoodsMsgService;
import io.renren.modules.shop.service.GoodsPicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("goods_msg")
@Api(value = "商品信息controller",tags = {("商品信息接口")})
public class GoodsMsgController {

    @Autowired
    private GoodsMsgService goodsMsgService;
    @Autowired
    private GoodsPicService goodsPicService;

    @ApiOperation(value = "查询分页")
    @PostMapping("page")
    public R getPage(@RequestBody GoodsMsg goodsMsg, @RequestParam Integer size, @RequestParam Integer page) {
        IPage<GoodsMsg> iPage = new Page<>(page,size);
        IPage<GoodsMsg> goodsCouponIPage = goodsMsgService.page(iPage, new QueryWrapper<>(goodsMsg));

        List<GoodsMsg> list = goodsCouponIPage.getRecords();
        for (GoodsMsg msg : list) {
            GoodsPic goodsPic = new GoodsPic();
            goodsPic.setGoodsId(msg.getGoodsId());
            GoodsPic one = goodsPicService.getOne(new QueryWrapper<>(goodsPic));
            msg.setPicPosition(Arrays.asList(one.getPicPath().split(",")));
        }
        goodsCouponIPage.setRecords(list);
        PageUtils pageResult = new PageUtils(goodsCouponIPage);
        return R.ok().put("page",pageResult);
    }

    @ApiOperation(value = "查询单条数据")
    @GetMapping("getById")
    public R getById (@RequestParam Integer id) {
        GoodsMsg goodsMsg = goodsMsgService.getById(id);
        GoodsPic goodsPic = new GoodsPic();
        goodsPic.setGoodsId(goodsMsg.getGoodsId());
        GoodsPic pic = goodsPicService.getOne(new QueryWrapper<>(goodsPic));
        String[] split = pic.getPicPath().split(",");
        goodsMsg.setPicPosition(Arrays.asList(split));
        return R.ok().put("entity", goodsMsg);
    }

    @ApiOperation(value = "根据条件查询列表")
    @PostMapping("list")
    public R getList(@RequestBody GoodsMsg goodsMsg) {
        List<GoodsMsg> list = goodsMsgService.list(new QueryWrapper<>(goodsMsg));
        for (GoodsMsg msg : list) {
            GoodsPic goodsPic = new GoodsPic();
            goodsPic.setGoodsId(msg.getGoodsId());
            GoodsPic one = goodsPicService.getOne(new QueryWrapper<>(goodsPic));
            msg.setPicPosition(Arrays.asList(one.getPicPath().split(",")));
        }
        return R.ok().put("list",list);
    }

    @ApiOperation(value = "随机获取查询列表")
    @GetMapping("getMore")
    public R getMore(@RequestBody GoodsMsg goodsMsg) {
        List<GoodsMsg> list = goodsMsgService.list(new QueryWrapper<>(goodsMsg));
        Collections.shuffle(list);
        for (GoodsMsg msg : list) {
            GoodsPic goodsPic = new GoodsPic();
            goodsPic.setGoodsId(msg.getGoodsId());
            GoodsPic one = goodsPicService.getOne(new QueryWrapper<>(goodsPic));
            msg.setPicPosition(Arrays.asList(one.getPicPath().split(",")));
        }
        return R.ok().put("list",list);
    }

    @ApiOperation(value = "获取不同类型的List")
    @GetMapping("allTypeList")
    public R getAllTypeList() {
        List<GoodsMsg> list0 = goodsMsgService.list(new QueryWrapper<GoodsMsg>().lambda().eq(GoodsMsg::getGoodsType, 0));
        for (GoodsMsg goodsMsg : list0) {
            GoodsPic goodsPic = new GoodsPic();
            goodsPic.setGoodsId(goodsMsg.getGoodsId());
            GoodsPic one = goodsPicService.getOne(new QueryWrapper<>(goodsPic));
            goodsMsg.setPicPosition(Arrays.asList(one.getPicPath().split(",")));
        }
        List<GoodsMsg> list1 = goodsMsgService.list(new QueryWrapper<GoodsMsg>().lambda().eq(GoodsMsg::getGoodsType, 1));
        for (GoodsMsg goodsMsg : list1) {
            GoodsPic goodsPic = new GoodsPic();
            goodsPic.setGoodsId(goodsMsg.getGoodsId());
            GoodsPic one = goodsPicService.getOne(new QueryWrapper<>(goodsPic));
            goodsMsg.setPicPosition(Arrays.asList(one.getPicPath().split(",")));
        }
        List<GoodsMsg> list2 = goodsMsgService.list(new QueryWrapper<GoodsMsg>().lambda().eq(GoodsMsg::getGoodsType, 2));
        for (GoodsMsg goodsMsg : list2) {
            GoodsPic goodsPic = new GoodsPic();
            goodsPic.setGoodsId(goodsMsg.getGoodsId());
            GoodsPic one = goodsPicService.getOne(new QueryWrapper<>(goodsPic));
            goodsMsg.setPicPosition(Arrays.asList(one.getPicPath().split(",")));
        }
        List<GoodsMsg> list3 = goodsMsgService.list(new QueryWrapper<GoodsMsg>().lambda().eq(GoodsMsg::getGoodsType, 3));
        for (GoodsMsg goodsMsg : list3) {
            GoodsPic goodsPic = new GoodsPic();
            goodsPic.setGoodsId(goodsMsg.getGoodsId());
            GoodsPic one = goodsPicService.getOne(new QueryWrapper<>(goodsPic));
            goodsMsg.setPicPosition(Arrays.asList(one.getPicPath().split(",")));
        }
        List<GoodsMsg> list4 = goodsMsgService.list(new QueryWrapper<GoodsMsg>().lambda().eq(GoodsMsg::getGoodsType, 4));
        for (GoodsMsg goodsMsg : list4) {
            GoodsPic goodsPic = new GoodsPic();
            goodsPic.setGoodsId(goodsMsg.getGoodsId());
            GoodsPic one = goodsPicService.getOne(new QueryWrapper<>(goodsPic));
            goodsMsg.setPicPosition(Arrays.asList(one.getPicPath().split(",")));
        }
        List<GoodsMsg> list5 = goodsMsgService.list(new QueryWrapper<GoodsMsg>().lambda().eq(GoodsMsg::getGoodsType, 5));
        for (GoodsMsg goodsMsg : list5) {
            GoodsPic goodsPic = new GoodsPic();
            goodsPic.setGoodsId(goodsMsg.getGoodsId());
            GoodsPic one = goodsPicService.getOne(new QueryWrapper<>(goodsPic));
            goodsMsg.setPicPosition(Arrays.asList(one.getPicPath().split(",")));
        }
        Map<String, List<GoodsMsg>> map = new HashMap<>();
        // 类别（0 最新上架 1.热门新品 2热门推荐 3猜你喜欢 4必买商品,5轮播商品）
        map.put("newList", list0);
        map.put("hotList", list1);
        map.put("recomadeList", list2);
        map.put("gussLikeList", list3);
        map.put("greatList", list4);
        map.put("lunbolist", list5);
        return R.ok().put("data", map);
    }

    @ApiOperation(value = "猜你喜欢单独接口")
    @GetMapping("guessLike")
    public R guessLike() {
        List<GoodsMsg> list3 = goodsMsgService.list(new QueryWrapper<GoodsMsg>().lambda().eq(GoodsMsg::getGoodsType, 3));
        for (GoodsMsg goodsMsg : list3) {
            GoodsPic goodsPic = new GoodsPic();
            goodsPic.setGoodsId(goodsMsg.getGoodsId());
            GoodsPic one = goodsPicService.getOne(new QueryWrapper<>(goodsPic));
            goodsMsg.setPicPosition(Arrays.asList(one.getPicPath().split(",")));
        }
        return R.ok().put("list", list3);
    }


    @ApiOperation(value = "根据ids删除货物记录")
    @PostMapping("delete")
    public R del(@RequestBody Integer[] ids) {
        for (Integer id : ids) {
            GoodsPic goodsPic = new GoodsPic();
            goodsPic.setGoodsId(id);
            goodsPicService.remove(new QueryWrapper<>(goodsPic));
        }
        boolean flag = goodsMsgService.removeByIds(Arrays.asList(ids));
        return R.ok().put("flag",flag);
    }


    @ApiOperation(value = "模糊搜索")
    @PostMapping("like")
    public R like(@RequestParam String goodsMsg) {
        List<GoodsMsg> list = goodsMsgService.list(new QueryWrapper<GoodsMsg>().lambda().like(GoodsMsg::getGoodsName, goodsMsg));
        for (GoodsMsg msg : list) {
            GoodsPic goodsPic = new GoodsPic();
            goodsPic.setGoodsId(msg.getGoodsId());
            GoodsPic one = goodsPicService.getOne(new QueryWrapper<>(goodsPic));
            msg.setPicPosition(Arrays.asList(one.getPicPath().split(",")));
        }
        return R.ok().put("list", list);
    }

}
